// preferences.js

var DEFAULTS_PREF_KEY = "extensions.appalachian.useDefault";
var DEFAULTS_EL_ID = "preferences-defaults-checkbox";
var SHOW_STATUS_PREF_KEY = "extensions.appalachian.showInStatusBar";
var SHOW_STATUS_EL_ID = "preferences-show-status-checkbox";
var DEBUG_PREF_KEY = "extensions.appalachian.debugEnabled";
var DEBUG_EL_ID = "preferences-debug-checkbox";

var AppalachianPrefs = {};

AppalachianPrefs.init = function() {
    if (!AppalachianPrefs.hasPref(DEFAULTS_PREF_KEY)) {
        AppalachianPrefs.setPref(DEFAULTS_PREF_KEY, true);
    }
    if (!AppalachianPrefs.hasPref(SHOW_STATUS_PREF_KEY)) {
        AppalachianPrefs.setPref(SHOW_STATUS_PREF_KEY, true);
    }
    if (!AppalachianPrefs.hasPref(DEBUG_PREF_KEY)) {
        AppalachianPrefs.setPref(DEBUG_PREF_KEY, false);
    }
};

AppalachianPrefs.onLoad = function() {
    AppalachianPrefs.init();

    var defaults = AppalachianPrefs.getPref(DEFAULTS_PREF_KEY);
    var statusbar = AppalachianPrefs.getPref(SHOW_STATUS_PREF_KEY);
    var debug = AppalachianPrefs.getPref(DEBUG_PREF_KEY);

    if (defaults) {
        var d = document.getElementById(DEFAULTS_EL_ID);
        d.setAttribute("checked", true);
    }

    if (statusbar) {
        var s = document.getElementById(SHOW_STATUS_EL_ID);
        s.setAttribute("checked", true);
    }

    if (debug) {
        var b = document.getElementById(DEBUG_EL_ID);
        b.setAttribute("checked", true);
    }
};

AppalachianPrefs.save = function() {
    var defaults = document.getElementById(DEFAULTS_EL_ID);
    var status = document.getElementById(SHOW_STATUS_EL_ID);
    var debug = document.getElementById(DEBUG_EL_ID);

    AppalachianPrefs.setPref(DEFAULTS_PREF_KEY, defaults.checked);
    AppalachianPrefs.setPref(SHOW_STATUS_PREF_KEY, status.checked);
    AppalachianPrefs.setPref(DEBUG_PREF_KEY, debug.checked);

    var par = window.opener;
    while (par.opener) { par = par.opener; }
    var icon = par.document.getElementById("appalachian-status-icon");
    if (icon) {
        if (status.checked)
            icon.style.display = 'block';
        else
            icon.style.display = 'none';
    }
};

AppalachianPrefs.getPref = function(key) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    return prefs.getBoolPref(key);
};

AppalachianPrefs.hasPref = function(key) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    return prefs.prefHasUserValue(key);
};

AppalachianPrefs.setPref = function(key, value) {
    var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    prefs.setBoolPref(key, value);  
};

AppalachianPrefs.getUsingDefault = function() {
    return AppalachianPrefs.getPref(DEFAULTS_PREF_KEY);
};

AppalachianPrefs.setUsingDefault = function(using) {
    AppalachianPrefs.setPref(DEFAULTS_PREF_KEY, using);
};

AppalachianPrefs.getShowInStatusBar = function() {
    return AppalachianPrefs.getPref(SHOW_STATUS_PREF_KEY);
};

AppalachianPrefs.setShowInStatusBar = function(show) {
    AppalachianPrefs.setPref(SHOW_STATUS_PREF_KEY, show);
};

AppalachianPrefs.getDebug = function() {
    return AppalachianPrefs.getPref(DEBUG_PREF_KEY);
};

AppalachianPrefs.setDebug = function(show) {
    AppalachianPrefs.setPref(DEBUG_PREF_KEY, show);
};

AppalachianPrefs.getString = function(key) {
    var bundle = document.getElementById("preferences-bundle");
    return bundle.getString(key);
};

AppalachianPrefs.reset = function() {
    var msg = AppalachianPrefs.getString("confirmreset");
    if (confirm(msg)) {
        AppalachianDB.wipe();
        AppalachianDB.init();
    }
};
